package com.softeem.dao.impl;

import com.softeem.bean.User;
import com.softeem.dao.UserDao;
import com.softeem.utils.BaseDao;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.SQLException;

public class UserDaoImpl extends BaseDao<User> implements UserDao {
    @Override
    public User queryUserByUsername(String username)throws SQLException {
        return this.getBean("select * from t_user where username = ?", username);
    }

    @Override
    public User queryUserByUsernameAndPassword(String username, String password)throws SQLException {
        return this.getBean("select * from t_user where username = ? and password = ?", username, password);
    }

    @Override
    public int saveUser(User user) throws SQLException{
        Integer id = 0;

            id = this.queryRunner.insert("insert into t_user values(null,?,?,?)",
                    new ScalarHandler<Long>(),
                    user.getUsername(), user.getPassword(), user.getEmail()).intValue();
            user.setId(id);//将添加数据成功后生成的主键id设置到user对象中

        return id;
    }
}
